sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel"
], function (Controller, JSONModel) {
    "use strict";
    var oJSONModel;
    return Controller.extend("sap.m.bbf41.Machining.Controller", {
        onInit: function () {
            var oView = this.getView();
            oView.byId('Shift1').setHeaderSpan([2, 2, 2]);
            oView.byId('Shift2').setHeaderSpan([2, 2, 2]);
            oView.byId('Shift3').setHeaderSpan([2, 2, 2]);
            oJSONModel = this.loadMachiningDataModel('G6');
            setInterval(function () {
                oView.setModel(oJSONModel);
            }, 1000);
        },
        handlePressOpenMenu: function (oEvent) {
            var oButton = oEvent.getSource();
            // create menu only once
            if (!this._menu) {
                this._menu = sap.ui.xmlfragment(
                    "sap.m.bbf41.Machining.MenuItemEventing",
                    this
                );
                this.getView().addDependent(this._menu);
            }
            var eDock = sap.ui.core.Popup.Dock;
            this._menu.open(this._bKeyboard, oButton, eDock.BeginTop, eDock.BeginBottom, oButton);
        },
        handleRefresh: function() {
            oJSONModel = this.loadMachiningDataModel();
            var oView = this.getView();
            oView.setModel(oJSONModel);
        },
        loadMachiningDataModel: function (lineName) {
            var me = this,
                oModel = new JSONModel(),
                oView = this.getView(),
                oTable = oView.byId('table');
            oTable.setBusy(true);
            jQuery.ajax({
                url: '/NEP/PSS/api/product_info?line_name=' + lineName,
                dataType: "json",
                success: function (resp) {
                    var oData = me.buildTableData(resp);
                    oModel.setData(oData);
                }.bind(this),
                error: function () {
                    jQuery.sap.log.error("failed to load json");
                },
                complete: function () {
                    oTable.setBusy(false);
                }
            });
            return oModel;
        },
        onNavButtonPress: function () {
            window.history.back();
        },
        handleMenuItemPress: function (oEvent) {
            var oMenuItem = oEvent.getSource(),
                text = oMenuItem.getText(),
                lineName;
            if (text == 'G6 - Crankcase') {
                this.getView().byId('page').setTitle('G6 - Crankcase');
                lineName = 'G6';
            }
            else {
                this.getView().byId('page').setTitle('Z6 - Cylinder Head');
                lineName = 'Z6';
            }
            oJSONModel = this.loadMachiningDataModel(lineName);
            var oPage = this.getView();
            oPage.setModel(oJSONModel);
            oPage.byId('table').bindRows("/Collection");
        },
        buildTableData: function (resp) {
            var data = {
                "Collection": [
                    {
                        "Type": "",
                        "Shift1Input": "Input",
                        "Shift1Output": "Output",
                        "Shift2Input": "Input",
                        "Shift2Output": "Output",
                        "Shift3Input": "Input",
                        "Shift3Output": "Output"
                    },
                    {
                        "Type": "Target",
                        "Shift1Input": "-",
                        "Shift1Output": "-",
                        "Shift2Input": "-",
                        "Shift2Output": "-",
                        "Shift3Input": "-",
                        "Shift3Output": "-"
                    },
                    {
                        "Type": "Current",
                        "Shift1Input": "-",
                        "Shift1Output": "-",
                        "Shift2Input": "-",
                        "Shift2Output": "-",
                        "Shift3Input": "-",
                        "Shift3Output": "-"
                    },
                    {
                        "Type": "Forecast",
                        "Shift1Input": "-",
                        "Shift1Output": "-",
                        "Shift2Input": "-",
                        "Shift2Output": "-",
                        "Shift3Input": "-",
                        "Shift3Output": "-"
                    },
                    {
                        "Type": "Planned",
                        "Shift1Input": "-",
                        "Shift1Output": "-",
                        "Shift2Input": "-",
                        "Shift2Output": "-",
                        "Shift3Input": "-",
                        "Shift3Output": "-"
                    },
                    {
                        "Type": "Accumulated",
                        "Shift1Input": "-",
                        "Shift1Output": "-",
                        "Shift2Input": "-",
                        "Shift2Output": "-",
                        "Shift3Input": "-",
                        "Shift3Output": "-"
                    }
                ],
                "Shift1StartTime": "-",
                "Shift1EndTime": "-",
                "Shift2StartTime": "-",
                "Shift2EndTime": "-",
                "Shift3StartTime": "-",
                "Shift3EndTime": "-",
                "Shift1": "Shift 1",
                "Shift2": "Shift 2",
                "Shift3": "Shift 3"
            };
            var po = data.Collection,
                setVal = function (v) {
                    return (Math.abs(v) > 0) ? v : '-';
                };

            if (resp) {
                data['Shift1StartTime'] = resp['S1'] || '-';
                data['Shift1EndTime'] = resp['E1'] || '-';
                data['Shift2StartTime'] = resp['S2'] || '-';
                data['Shift2EndTime'] = resp['E2'] || '-';
                data['Shift3StartTime'] = resp['S3'] || '-';
                data['Shift3EndTime'] = resp['E3'] || '-';
                po[1]['Shift1Input'] = setVal(resp['TargetInput1']);
                po[1]['Shift1Output'] = setVal(resp['TargetOutput1']);
                po[1]['Shift2Input'] = setVal(resp['TargetInput2']);
                po[1]['Shift2Output'] = setVal(resp['TargetOutput2']);
                po[1]['Shift3Input'] = setVal(resp['TargetInput3']);
                po[1]['Shift3Output'] = setVal(resp['TargetOutput3']);

                po[2]['Shift1Input'] = setVal(resp['CurrentInput1']);
                po[2]['Shift1Output'] = setVal(resp['CurrentOutput1']);
                po[2]['Shift2Input'] = setVal(resp['CurrentInput2']);
                po[2]['Shift2Output'] = setVal(resp['CurrentOutput2']);
                po[2]['Shift3Input'] = setVal(resp['CurrentInput3']);
                po[2]['Shift3Output'] = setVal(resp['CurrentOutput3']);

                po[2]['Shift1Input'] = setVal(resp['CurrentInput1']);
                po[2]['Shift1Output'] = setVal(resp['CurrentOutput1']);
                po[2]['Shift2Input'] = setVal(resp['CurrentInput2']);
                po[2]['Shift2Output'] = setVal(resp['CurrentOutput2']);
                po[2]['Shift3Input'] = setVal(resp['CurrentInput3']);
                po[2]['Shift3Output'] = setVal(resp['CurrentOutput3']);

                po[3]['Shift1Input'] = setVal(resp['ForecastInput1']);
                po[3]['Shift1Output'] = setVal(resp['ForecastOutput1']);
                po[3]['Shift2Input'] = setVal(resp['ForecastInput2']);
                po[3]['Shift2Output'] = setVal(resp['ForecastOutput2']);
                po[3]['Shift3Input'] = setVal(resp['ForecastInput3']);
                po[3]['Shift3Output'] = setVal(resp['ForecastOutput3']);

                po[4]['Shift1Input'] = setVal(resp['PlannedInput1']);
                po[4]['Shift1Output'] = setVal(resp['PlannedOutput1']);
                po[4]['Shift2Input'] = setVal(resp['PlannedInput2']);
                po[4]['Shift2Output'] = setVal(resp['PlannedOutput2']);
                po[4]['Shift3Input'] = setVal(resp['PlannedInput3']);
                po[4]['Shift3Output'] = setVal(resp['PlannedOutput3']);

                po[5]['Shift1Input'] = (resp['CurrentInput1'] && resp['PlannedInput1']) ? setVal(parseInt(resp['CurrentInput1']) - parseInt(resp['PlannedInput1'])) : '-';
                po[5]['Shift1Output'] = (resp['CurrentOutput1'] && resp['PlannedOutput1']) ? setVal(parseInt(resp['CurrentOutput1']) - parseInt(resp['PlannedOutput1'])) : '-';
                po[5]['Shift2Input'] = (resp['CurrentInput2'] && resp['PlannedInput2']) ? setVal(parseInt(resp['CurrentInput2']) - parseInt(resp['PlannedInput2'])) : '-';
                po[5]['Shift2Output'] = (resp['CurrentOutput2'] && resp['PlannedOutput2']) ? setVal(parseInt(resp['CurrentOutput2']) - parseInt(resp['PlannedOutput2'])) : '-';
                po[5]['Shift3Input'] = (resp['CurrentInput3'] && resp['PlannedInput3']) ? setVal(parseInt(resp['CurrentInput3']) - parseInt(resp['PlannedInput3'])) : '-';
                po[5]['Shift3Output'] = (resp['CurrentOutput3'] && resp['PlannedOutput3']) ? setVal(parseInt(resp['CurrentOutput3']) - parseInt(resp['PlannedOutput3'])) : '-';
            }
            return data;
        }
    });

});
